package dormouse.vfs.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.VFS;

import dormouse.vfs.event.IFileSelectionEvent;
import dormouse.vfs.event.IFileSelectionListener;
import dormouse.vfs.ui.actions.SelectedFileList;
import dormouse.vfs.ui.renderers.FileRendererPanel;
import dormouse.vfs.ui.renderers.IFileListRenderer;

/**
 * This class provides a simple user interface that displays a tree
 * of directories and files.
 */
public class FileTreeTable extends JPanel {

    /** Serial UID. */
	private static final long serialVersionUID = 8271780028294022107L;
	
	protected FileTree fileTree = null;
    protected FileRendererCardStack fileCardStack = null;
    protected JScrollPane leftScroller = new JScrollPane();
    protected JScrollPane rightScroller = new JScrollPane();
    protected FileObject dir = null;
    protected JSplitPane pane = new JSplitPane();
    protected JPanel leftPanel = new JPanel();
    protected FileRendererPanel preview = new FileRendererPanel();
    protected SelectedFileList selectedFiles = new SelectedFileList();

    /**
     * Constructor
     * @param file the root directory
     * @throws FileSystemException 
     */
    public FileTreeTable(String file) throws FileSystemException {
        this(VFS.getManager().resolveFile(file));
    }

    /**
     * Constructor
     * @throws FileSystemException 
     */
    public FileTreeTable() throws FileSystemException {
        this(System.getProperty("user.home"));
    }

    /**
     * Constructor
     * @param dir the root directory
     */
    public FileTreeTable(FileObject dir) {
        super();
        this.dir = dir;
        initialize();

    }

    public List<IFileListRenderer<Component>> getViewList() {
        return this.fileCardStack.getRendererNameList();
    }

    public void setCurrentView(String view) {
        this.fileCardStack.setCurrentRenderer(view);
    }

    /**
     * This method initializes this
     * 
     */
    private void initialize() {
        fileTree = new FileTree(dir);
        fileCardStack = new FileRendererCardStack(dir);

        leftScroller.setViewportView(fileTree);
        rightScroller.setViewportView(fileCardStack);
        leftPanel.setLayout(new BorderLayout());
        leftPanel.add(leftScroller, BorderLayout.CENTER);
        leftPanel.add(preview, BorderLayout.SOUTH);

        pane.setLeftComponent(leftPanel);
        pane.setRightComponent(rightScroller);
        pane.setDividerLocation(200);
        pane.setOneTouchExpandable(true);

        // add items to layout
        this.setLayout(new BorderLayout());
        this.add(pane, BorderLayout.CENTER);

        // respond to directory selection events from the tree
        fileTree.addTreeSelectionListener(new TreeSelectionListener() {

            public void valueChanged(TreeSelectionEvent tse) {
                FileNode node = (FileNode) fileTree.getLastSelectedPathComponent();
                if (node == null) {
                    return;
                }

                SelectableFile dir = (SelectableFile) node.getUserObject();
                fileCardStack.setDirectory(dir.getFile());
                node.explore();

            }
        });

        // respond to file selection events from the IFileListRenderer
        fileCardStack.addFileSelectionListener(new IFileSelectionListener() {

            public void fileSelected(IFileSelectionEvent event) {
            	FileObject file = event.getSelectedFile();
                preview.render(file);
                System.out.println("rendering file: " + file.getName().getBaseName());
            }
        });



    }

    public void setDirectory(FileObject dir) {
        this.fileCardStack.setDirectory(dir);
        this.fileTree.setSelectedDir(dir);
    }

    public void addTreeSelectionListener(TreeSelectionListener tsl) {
        fileTree.addTreeSelectionListener(tsl);
    }

    public List getRendererNameList() {
        return this.fileCardStack.getRendererNameList();
    }
}  

